//
//  ST_BT2KBarrageAdapter.swift
//  BTVideo
//
//  Created by DouLei on 2019/7/15.
//  Copyright © 2019 DouLei. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwifterSwift
import HandyJSON

let barrageLabelHeight = 28.cgFloat

class ST_BT2KBarrageAdapter: NSObject {

//    MARK: - shared
    static let shared = ST_BT2KBarrageAdapter()
    
    private override init() {
        super.init()
        
//        print("iOS 所有字体:",UIFont.familyNames)
    }
    
    //    请求到数据 返回给外部 id
//    var playBarrageBlock:((String)->())?
    
    private var reqSet = Set<String>()
    
    private var top_barrage_caches = [String:[ST_BarrageModel]]()
    
    private var middle_barrage_caches = [String:[ST_BarrageModel]]()
    
    private var bottom_barrage_caches = [String:[ST_BarrageModel]]()
//    MARK: - private method
    private func request_barrage(videoId:String){
        
        
        let param = ["video_id":videoId]
        
        JVrequest(PH.getBarrageApi,parameters:param ,success: { (json) in
        
            let data = json["data"]["list"]
            
            print("打印出来看 弹幕",data)
            
            if let list = [ST_BarrageModel].deserialize(from: data["normal"].arrayObject){
                
                let existArray = list.compactMap({$0})
                
                self.reqSet.update(with: videoId)
                
                self.top_barrage_caches.updateValue(existArray, forKey: videoId)
                
            }
            
            if let list = [ST_BarrageModel].deserialize(from: data["above"].arrayObject){
                
                let existArray = list.compactMap({$0})
                
                self.reqSet.update(with: videoId)
                
                self.middle_barrage_caches.updateValue(existArray, forKey: videoId)
                
            }
            
//            let st =  data["above"].arrayObject
            
//            print("底部的弹幕---",st)
            
            if let list = [ST_BarrageModel].deserialize(from: data["below"].arrayObject){
                
                let existArray = list.compactMap({$0})
                
                self.reqSet.update(with: videoId)
                
                self.bottom_barrage_caches.updateValue(existArray, forKey: videoId)
                
            }
            
            NotificationCenter.default.post(name: Notification.Name.ready_play_barrage, object: nil, userInfo: ["videoId" : videoId])
            
            
        }) { (code, str) in
            print(str)
        }   
        
    }
//    MARK: - public method
    func add_will_req(videoId:String) {
        
//        if !reqSet.contains(videoId){
            request_barrage(videoId: videoId)
//        }
    }
    
    
    func find_top_barrages(videoId:String)->[ST_FWBarrageItemModel]?{
        
        if let list = top_barrage_caches[videoId]{
            
            return generate_barrage_item(list: list)
            
        }else{
            return nil
        }
    }
    
    func find_middle_barrages(videoId:String)->[ST_FWBarrageItemModel]?{
        
        if let list = middle_barrage_caches[videoId]{

            return generate_barrage_item(list: list)

        }else{
            return nil
        }
    }
    
    func find_bottom_barrages(videoId:String)->[ST_FWBarrageItemModel]?{
            
        if let list = bottom_barrage_caches[videoId]{

            return generate_barrage_item(list: list)

        }else{
            return nil
        }
    }
    
    
    
    private func generate_barrage_item(list:[ST_BarrageModel])->[ST_FWBarrageItemModel]{
        
        var data_array = [ST_FWBarrageItemModel]()
                            
        for item in list {
        
            let m = ST_CompatibilityBarrageItem(item: item)
            
            data_array.append(m)
        }
        
        return data_array
    }
    
    
    
}


//MARK: -
class ST_BarrageModel:HandyJSON{
    
    var udid = UUID().uuidString
    
    var reply_content = ""
    //    16进制 ex: FFFFFF
    var reply_color = ""
    
    var is_vip = false
    
    var avatar = ""
    
    required init() {
    }
    
    static func convert_from(editBarrage:ST_EditBarrageModel)->ST_BarrageModel{
        
        let barrage = ST_BarrageModel()
        
        barrage.reply_color = editBarrage.color.hexString
        
        barrage.reply_content = editBarrage.text
        
        if editBarrage.color.hexString != UIColor.white.hexString{
            barrage.is_vip = true
            let avatar = ST_BTUserInfoManager.shared.current_account_info?.userinfo?.avatar ?? ""
            barrage.avatar = avatar
        }
        
        return barrage
    }
}
